From 8ca796ea999b32a7525caddb3ed2febdf53b5bda Mon Sep 17 00:00:00 2001 From: Matthias Mullie Date: Mon, 28 Sep 2015 09:55:28 +0200 Subject: [PATCH] Fix Memcached key decode Flow had a key: flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7 the '+' in there was not being encoded (it only does /[\x00-\x20\x25\x7f]+/) but coming back, it was decoded into ' '. getMulti() shows a key=>value array or results. Since key was different, we couldn't find what we had requested. Bug: T110326 Change-Id: Ia92edd73d0eb7fe0d35e38e7e7af8174fb85cbcc --- includes/objectcache/MemcachedBagOStuff.php | 5 ++++- tests/phpunit/includes/objectcache/BagOStuffTest.php | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/includes/objectcache/MemcachedBagOStuff.php b/includes/objectcache/MemcachedBagOStuff.php index e545aa55a6..7d1274921c 100644 --- a/includes/objectcache/MemcachedBagOStuff.php +++ b/includes/objectcache/MemcachedBagOStuff.php @@ -168,7 +168,10 @@ class MemcachedBagOStuff extends BagOStuff { * @return string */ public function decodeKey( $key ) { - return urldecode( $key ); + // matches %00-%20, %25, %7F (=decoded alternatives for those encoded in encodeKey) + return preg_replace_callback( '/%([0-1][0-9]|20|25|7F)/i', function ( $match ) { + return urldecode( $match[0] ); + }, $key ); } /** diff --git a/tests/phpunit/includes/objectcache/BagOStuffTest.php b/tests/phpunit/includes/objectcache/BagOStuffTest.php index b684006202..cde7ed680e 100644 --- a/tests/phpunit/includes/objectcache/BagOStuffTest.php +++ b/tests/phpunit/includes/objectcache/BagOStuffTest.php @@ -139,24 +139,28 @@ class BagOStuffTest extends MediaWikiTestCase { $value1 = array( 'this' => 'is', 'a' => 'test' ); $value2 = array( 'this' => 'is', 'another' => 'test' ); $value3 = array( 'testing a key that may be encoded when sent to cache backend' ); + $value4 = array( 'another test where chars in key will be encoded' ); $key1 = wfMemcKey( 'test1' ); $key2 = wfMemcKey( 'test2' ); $key3 = wfMemcKey( 'will-%-encode' ); // internally, MemcachedBagOStuffs will encode to will-%25-encode + $key4 = wfMemcKey( 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7' ); $this->cache->add( $key1, $value1 ); $this->cache->add( $key2, $value2 ); $this->cache->add( $key3, $value3 ); + $this->cache->add( $key4, $value4 ); $this->assertEquals( - array( $key1 => $value1, $key2 => $value2, $key3 => $value3 ), - $this->cache->getMulti( array( $key1, $key2, $key3 ) ) + array( $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ), + $this->cache->getMulti( array( $key1, $key2, $key3, $key4 ) ) ); // cleanup $this->cache->delete( $key1 ); $this->cache->delete( $key2 ); $this->cache->delete( $key3 ); + $this->cache->delete( $key4 ); } /** -- 2.20.1